package com.e2oto.seller.common.utils;


import com.e2oto.seller.common.Config;
import com.e2oto.seller.common.base.BaseBean;
import com.e2oto.seller.common.download.DownLoadService;
import com.e2oto.seller.common.utils.fastjson.FastJsonConverterFactory;
import com.e2oto.seller.common.http.ApiService;
import com.e2oto.seller.common.http.ProgressSubscriber;
import com.e2oto.seller.common.http.StateLayoutSubscriber;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;


/**
 * Created by ZhangLiQiang on 2017/8/1.
 */

public class RetrofitUtils {


    private volatile static Retrofit retrofit;


    public static ApiService getApiService() {
        return getInstance().create(ApiService.class);
    }

    public static DownLoadService getDownLoadService() {
        return getInstance().create(DownLoadService.class);
    }

    private static Retrofit getInstance() {
        if (retrofit == null) {
            synchronized (Retrofit.class) {
                if (retrofit == null) {
                    Retrofit.Builder builder = new Retrofit.Builder();
                    retrofit = builder.baseUrl(Config.APP_HOST)
                            .client(getOkHttpClient())
                            .addConverterFactory(FastJsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .build();
                }
            }
        }
        return retrofit;
    }


    private static OkHttpClient getOkHttpClient() {
        //日志显示级别
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                LogTools.e("http start............................................");
                LogTools.e(message);
                LogTools.e("http end................................................");
            }
        });
        loggingInterceptor.setLevel(level);
        //定制OkHttp
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient
                .Builder();
        //OkHttp进行添加拦截器loggingInterceptor
        httpClientBuilder.addInterceptor(loggingInterceptor);

        return httpClientBuilder
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
    }


    /***
     * 封装网络请求 带progressbar
     * @param observable
     * @param subscriber
     */
    public static void Request(Observable<BaseBean> observable, final ProgressSubscriber subscriber) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /***
     * 封装网络请求
     * @param observable
     * @param subscriber
     */
    public static void Request(Observable<BaseBean> observable, final StateLayoutSubscriber subscriber) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /***
     * 封装网络请求
     * @param observable
     * @param subscriber
     */
    public static void Request(Observable<BaseBean> observable, final Consumer subscriber) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
